<style scoped>
    .inquiry-list-page{}

</style>
<template> 
    <div class="inquiry-list-page">
        <el-form :inline="true" :model="formInline" :rules="rules" ref="formInline" class="demo-form-inline" style="border-bottom:1px solid #ccc;">
            <!--法人名称-->
            <el-form-item :label="nowLangue.legalName" class="selectMB10">
                  <el-select size="mini" clearable  v-model="formInline.legalPersonName" :placeholder="nowLangue.pleaseSelect" @change="changeLegalPerson">
                    <el-option v-for="(legalPerson,index) in legalPersonDropDown" :key="index" :label="legalPerson.legalPersonName" :value="legalPerson.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <!--询价单号-->
            <el-form-item :label="nowLangue.inquiryNo" class="selectMB10">
                <el-input size="mini" v-model="formInline.requestForQuotationId" :placeholder="nowLangue.pleaseImport+nowLangue.inquiryNo"></el-input>
            </el-form-item>
            <!--询价日期-->
            <el-form-item :label="nowLangue.inquiryDate" class="selectMB10">
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.createStartTime"  @change="selectedCreateStartTime" :placeholder="nowLangue.pleaseSelect" ></el-date-picker> {{nowLangue.to}}
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.createEndTime" @change="selectedCreateEndTime" :placeholder="nowLangue.pleaseSelect" ></el-date-picker>
            </el-form-item>
            <!--询价开始日期-->
            <el-form-item :label="nowLangue.inquiryStartDate" class="selectMB10">
                <!--  prop="startTime"  :rules="[{ required: true, message: '询价开始日期不能为空'}]" -->
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.startTime" :placeholder="nowLangue.pleaseSelect" class="selectMB10" prop="startTime" @change="selectedStartTime"></el-date-picker>
            </el-form-item>
            <!--询价结束日期-->
            <el-form-item :label="nowLangue.inquiryEndDate" class="selectMB10">
                <!--  prop="endTime" :rules="[{ required: true, message: '询价结束日期不能为空'}]"  -->
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.endTime" :placeholder="nowLangue.pleaseSelect" class="selectMB10" prop="endTime"  @change="selectedEndTime"></el-date-picker>
            </el-form-item>
            <!--供应商名称-->
            <el-form-item :label="nowLangue.supplierName" class="selectMB10">
                <el-select size="mini" clearable  v-model="formInline.vendor" :placeholder="nowLangue.pleaseSelect" prop="supplierName">
                    <el-option v-for="(vendor,index) in formInline.supplierDropdown" :key="index" :label="vendor.partnerName" :value="vendor.partnerCode"></el-option>
                </el-select>
            </el-form-item>
            <!--询价类型-->
            <el-form-item :label="nowLangue.inquiryType" class="selectMB10">
                <el-select size="mini" clearable v-model="formInline.type" :placeholder="nowLangue.pleaseSelect" >
                    <el-option v-for="(type,index) in selectDropdown.type" :key="index" :label="type.label" :value="type.value"></el-option>
                </el-select>
            </el-form-item>
            <!--询价状态-->
             <el-form-item :label="nowLangue.inquiryStatus" class="selectMB10">
                <el-select size="mini" clearable v-model="formInline.inquiryStatus" :placeholder="nowLangue.pleaseSelect">
                    <el-option v-for="(inquiryStatus,index) in selectDropdown.inquiryStatus" :key="index" :label="inquiryStatus.label" :value="inquiryStatus.value"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="selectMB10">
                <!--查询-->
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="queryInquirySheet()" v-text="nowLangue.query"></el-button>
            </el-form-item>
        </el-form>
        <!--表格-->
        <!-- 询价单列表： -->
        <label class="default-label" v-text="nowLangue.listOfInquiry"></label>
        <!--创建询价单按钮-->
        <el-button class="bolder outsideBtnBS" size="mini" type="danger" v-if="powerFlag1" @click="createNewInq" v-text="nowLangue.create"></el-button>
        <el-table :data="inquirySheetData" id="inquirySheet" border style="width: 100%;" v-loading.body="loading" element-loading-text="拼命加载中" :default-sort = "{prop: 'date', order: 'descending'}">
            <!--序-->
            <el-table-column type="index" header-align="center" align="center" :label="nowLangue.sequence"  width="40"></el-table-column>
            <!--操作-->
            <el-table-column :label="nowLangue.operate" header-align="center" align="center" width="184" show-overflow-tooltip>
                <template scope="scope">
                    <!--修改-->
                    <div class="operationItem bolder"  v-if="scope.row.inquiryStatus==10 && powerFlag1" @click="handleUpdate(scope.$index,scope.row)" v-text="nowLangue.update"></div>
                    <!--修改-->
                    <div class="operationItem disabled bolder"  v-else-if="scope.row.inquiryStatus!=10 && powerFlag1" v-text="nowLangue.update"></div>
                    <!--审核-->
                    <div class="operationItem bolder"  v-if="scope.row.inquiryStatus==20 && powerFlag2" @click="handleExamine(scope.$index,scope.row)" v-text="nowLangue.toExamine"></div>
                    <!--审核-->
                    <div class="operationItem disabled bolder"  v-else-if="scope.row.inquiryStatus!=20 && powerFlag2" v-text="nowLangue.toExamine"></div>
                    <!--审批-->
                    <div class="operationItem bolder"  v-if="scope.row.inquiryStatus==30 && powerFlag3" @click="handleApproval(scope.$index,scope.row)" v-text="nowLangue.approval"></div>
                    <!--审批-->
                    <div class="operationItem bolder disabled"  v-else-if="scope.row.inquiryStatus!=30 && powerFlag3" v-text="nowLangue.approval"></div>
                    <!--作废-->
                    <div class="operationItem bolder"  v-if="scope.row.inquiryStatus==10 && powerFlag4" @click="handleVoid(scope.$index, scope.row)" v-text="nowLangue.toVoid"></div>
                    <!--作废-->
                    <div class="operationItem bolder disabled"  v-else-if="scope.row.inquiryStatus!=10 && powerFlag4" v-text="nowLangue.toVoid"></div>
                    <div class="operationItem bolder">
                        <!--查看-->
                        <router-link :to="{ path: 'SeeInquirySheet', query: { requestForQuotationId: scope.row.requestForQuotationId}}" class="operationItem bolder" v-text="nowLangue.lookUp"></router-link>
                    </div>
                </template>
            </el-table-column>
            <!--询价单号-->
            <el-table-column prop="requestForQuotationId" :label="nowLangue.inquiryNo" header-align="center" width="100" show-overflow-tooltip></el-table-column>
            <!--询价日期-->
            <el-table-column prop="createTime" :label="nowLangue.inquiryDate" header-align="center" width="100" show-overflow-tooltip></el-table-column>
            <!--询价开始日期-->
            <el-table-column prop="startTime" :label="nowLangue.inquiryStartDate" header-align="center" width="100" show-overflow-tooltip></el-table-column>
            <!--询价结束日期-->
            <el-table-column prop="endTime" :label="nowLangue.inquiryEndDate" header-align="center" width="100" show-overflow-tooltip></el-table-column>
            <!--状态-->
            <el-table-column prop="inquiryStatusName" :label="nowLangue.status" header-align="center" align="center" width="80"></el-table-column>
            <!--询价人员-->
            <el-table-column prop="creatorName" :label="nowLangue.inquiryPersonnel" header-align="center" width="120" show-overflow-tooltip></el-table-column>
            <!--法人名称-->
            <el-table-column prop="legalPersonName" :label="nowLangue.legalName" header-align="center" show-overflow-tooltip></el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>
    </div>
</template>

<script>
       export default {
        data() {
            var checkstartTime = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('询价开始时间不能为空'));
                }
            };
            var checkendTime = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('询价结束时间不能为空'));
                }
            };
            return {
                nowLangue:{},
                // 查询表单
                formInline:{
                    requestForQuotationId:"",
                    legalPersonId:"",
                    legalPersonName:"",
                    createTime:"",
                    createStartTime:"",
                    createEndTime:"",
                    startTime:"",
                    endTime:"",
                    vendor:'',
                    inquiryStatus:"",
                    type:"",
                    supplierDropdown:[]
                },
                loading:false,
                disabled:false,
                inquirySheetData: [],// 询价单表格
                legalPersonDropDown:[], //法人名称
                supplierNameDropDown:[], //供应商名称
                selectDropdown:[],     //查询下拉菜单
                //inquiryTypeDropdown:[],    //询价类型
                //inquiryStatusDropdown:[],  //询价状态


                // 分页
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                totalNumber: 20,
                totalPage:2,
                isPaging:false,
                // orderStatus:"",    //询价单状态   为50时，已作废
                rules: {
                    startTime: [
                        { validator: checkstartTime, trigger: 'change' }
                        // { required: true, message: '请输入询价开始日期', trigger: 'blur' },
                        // // { min: 1, max: 100, message: '长度在1到100个字符', trigger: 'blur' },
                        // { validator: inquiryBeginsDate, trigger: 'blur' }
                    ],
                    endTime: [
                        { validator: checkendTime, trigger: 'change' }
                        // { required: true, message: '请输入询价结束日期', trigger: 'blur' },
                        // // { min: 1, max: 20, message: '长度在1到20个字符', trigger: 'blur' },
                        // { validator: inquiryEndDate, trigger: 'blur' }
                    ]
                },
                //权限
                powerFlag1:false,
                powerFlag2:false,
                powerFlag3:false,
                powerFlag4:false,
                //查询禁用
                selectDisabled:false

            };
        },
        created:function(){
            var _this=this;
            _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
        },
        computed:{
            powerData(){
                return this.$store.state.powerData;
            },
            languageType(){
                return this.$store.state.languageType;
            }
        },
        watch:{
            languageType(){
                var _this=this;
                _this.languageType=="zh"?_this.nowLangue=_this.language.zh:_this.nowLangue=_this.language.en;
            },
            powerData(){
                var _this=this;
                for(var i=0;i<_this.powerData.length;i++){
                    if(_this.powerData[i]=="ROLE_security_inquiryList_modify"){
                        //询价清单创建、修改、保存、提交权限
                        _this.powerFlag1=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_inquiryList_review"){
                        //询价清单审核权限
                        _this.powerFlag2=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_inquiryList_approve"){
                        //询价清单审批权限
                        _this.powerFlag3=true;
                    }
                    if(_this.powerData[i]=="ROLE_security_inquiryList_abolish"){
                        //询价清单作废权限
                        _this.powerFlag4=true;
                    }
                }
            }

        },
       //进入页面立即运行此方法
       mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'InquirySheet');
            //判断权限
            for(var i=0;i<_this.powerData.length;i++){
               if(_this.powerData[i]=="ROLE_security_inquiryList_modify"){
                   //询价清单创建、修改、保存、提交权限
                   _this.powerFlag1=true;
               }
               if(_this.powerData[i]=="ROLE_security_inquiryList_review"){
                   //询价清单审核权限
                   _this.powerFlag2=true;
               }
               if(_this.powerData[i]=="ROLE_security_inquiryList_approve"){
                   //询价清单审批权限
                   _this.powerFlag3=true;
               }
               if(_this.powerData[i]=="ROLE_security_inquiryList_abolish"){
                   //询价清单作废权限
                   _this.powerFlag4=true;
               }
            }
            // //ajax前后查询按钮禁用
            // $(".selectAll").ajaxStart(function(){
            //    _this.selectDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //    _this.selectDisabled=false;
            // });
            _this.legalPersonDropDownValue(); //法人名称下拉列表值
            _this.inquiryDropdownValue();  //询价类型和询价状态下拉列表
            _this.queryInquirySheet();    //表单查询
       },
        methods: {
            createNewInq(){
              var _this=this;
                _this.$router.push({path:"/CreateInquirySheet"});
            },
            //询价日期
            selectedCreateStartTime(val){
                var _this=this;
                _this.formInline.createStartTime = val;
            },
            selectedCreateEndTime(val){
                var _this=this;
                _this.formInline.createEndTime = val;
            },
            //询价开始日期
            selectedStartTime(val){
                var _this=this;
                _this.formInline.startTime = val;   
                if(_this.formInline.endTime && val > _this.formInline.endTime){
                    var err=_this.languageType=="zh"?'询价开始日期不能大于询价结束日期':'The starting date of inquiry cannot be greater than the end date of inquiry';
                    _this.common.errorTip(err);
                    _this.formInline.startTime = "";
                };             
            },
            // 询价结束日期
            selectedEndTime(val){
                var _this=this;
                _this.formInline.endTime = val;
                if(_this.formInline.startTime && val < _this.formInline.startTime){
                    var err=_this.languageType=="zh"?'询价结束日期不能小于询价开始日期':'The closing date of inquiry shall not be less than the date of inquiry';
                    _this.common.errorTip(err);
                    _this.formInline.endTime = "";
                };
            },
            // 修改按钮
            handleUpdate(index,row){
                console.log(row);
                var _this=this;
                _this.$router.push({path:"/ModifyInquirySheet",query:{requestForQuotationId:row.requestForQuotationId}});
            },
             // 审核按钮
            handleExamine(index,row){
                var _this=this;
                _this.$router.push({path:"/SeeInquirySheet",query:{requestForQuotationId:row.requestForQuotationId}});
            },
            // 审批按钮
            handleApproval(index,row){
                var _this=this;
                _this.$router.push({path:"/SeeInquirySheet",query:{requestForQuotationId:row.requestForQuotationId}});
            },
            // // 作废按钮
            // handleVoid(index,row){
            //     var _this=this;
            //     _this.$router.push({path:"/SeeInquirySheet",query:{requestForQuotationId:row.requestForQuotationId}});
            // },
            //法人名称下拉列表值
            legalPersonDropDownValue(){  
                var _this = this;
                $.ajax({
                    type:'GET',
                    url:_this.procurement_service+"legalPerson/ofCurrentUser",
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.legalPersonDropDown = data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            // 选择法人 取id
            changeLegalPerson(){
                var _this = this;
                for(var i=0;i<_this.legalPersonDropDown.length;i++){
                    if(_this.formInline.legalPersonName==_this.legalPersonDropDown[i].id){
                        _this.formInline.legalPersonId=_this.legalPersonDropDown[i].id;
                    }
                }
               _this.dropdownValue();
            },
            //供应商下拉列表值
            dropdownValue(){  
                var _this=this;
                $.ajax({
                    type:'GET',
                    url:_this.metadata_service+"partner/queryByLegalPerson?legalPersonId="+_this.formInline.legalPersonId,
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.formInline.supplierDropdown=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },
            //询价类型和询价状态
            inquiryDropdownValue(){  
                var _this = this;
                var tmp=["type","inquiryStatus"];
                $.ajax({
                    type:'POST',
                    url:_this.procurement_service+'procurement/getPullDownList',
                    cache:false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType:"application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            _this.selectDropdown=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },

            // 查询按钮   查询询价清单
            queryInquirySheet(){
                var _this=this;
                _this.loading=true;
                var tmp={
                    requestForQuotationId:_this.formInline.requestForQuotationId,
                    legalPersonId:_this.formInline.legalPersonId,
                    createTime:_this.formInline.createTime,  // 创建时间（也就是询价日期）
                    createStartTime:_this.formInline.createStartTime,//创建开始时间（条件查询所需）
                    createEndTime:_this.formInline.createEndTime,//创建结束时间（条件查询所需）
                    startTime:_this.formInline.startTime, //询价开始时间
                    endTime:_this.formInline.endTime, //询价结束时间
                    vendor:_this.formInline.vendor, //供应商名称
                    inquiryStatus:_this.formInline.inquiryStatus, //询价状态
                    type:_this.formInline.type, //询价类型
                    pageIndex:_this.pageIndex,
                    pageSize:_this.pageSize
                };
                _this.selectDisabled=true;
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"inquiryList/search",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            _this.totalPage=data.data.totalPage;
                            _this.totalNumber=data.data.totalNumber;
                            _this.inquirySheetData=data.data.dataList;
                        }else{
                            _this.common.errorTip(data.errorMessage)
                        }
                        _this.selectDisabled=false;
                    }
                });
            },
            // 作废按钮
            handleVoid(index, row) {
                // console.log(index,row);
                var _this=this;
                var tmp ={
                    requestForQuotationId:row.requestForQuotationId,
                    orderStatus:50
                };
                // console.log(tmp);
                $.ajax({
                    type: 'POST',
                    url:_this.procurement_service+"inquiryList/modifyState",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success:function(data){
                        if(data.success){
                            // _this.orderStatus=data.data.orderStatus;
                            for(var i=0;i<_this.inquirySheetData.length;i++){
                                if(_this.inquirySheetData[i].requestForQuotationId == row.requestForQuotationId){
                                    _this.inquirySheetData[i].inquiryStatus=50;
                                }
                            }
                            row.inquiryStatusName="已报废";
                            // _this.inquirySheetData.inquiryStatus=data.data.orderStatus;
                            // console.log("_this.orderStatus="+_this.orderStatus);
                            var success=_this.languageType=="zh"?'操作成功':'Successful operation';
                            _this.common.successTip(success);
                        }else{

                        }
                    }
                })
            },

            //   分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.pageSize=val;
                _this.queryInquirySheet();

            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.pageIndex=val;
                _this.queryInquirySheet();
            },
            // // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: this.nowLangue.success,
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: this.nowLangue.error,
            //         message: p
            //     });
            // }

        }
    }
</script>



